$:.unshift File.join(File.dirname(__FILE__), "..", "lib")
require 'UPnP/service/content_directory'

class TestContentDirectoryItem < Test::Unit::TestCase
  # A
  #  e
  #  C
  #   d
  #  B
  #   b
  #   c
  #   a
  #   D
  def test_structure
        
    container = UPnP::Service::Container.root_container

    ca = UPnP::Service::Container.new("A")
    cb = UPnP::Service::Container.new("B")
    cc = UPnP::Service::StorageFolder.new("C")
    cd = UPnP::Service::StorageFolder.new("D")
    
    ia = UPnP::Service::Item.new("a")
    ib = UPnP::Service::VideoItem.new("b")
    ic = UPnP::Service::Movie.new("c")
    id = UPnP::Service::VideoBroadcast.new("d")
    ie = UPnP::Service::Photo.new("e")
    
    container << ca
    ca << ie
    ca << cc << cb
    
    cc << id
    cb << ib << ic << ia << cd
    
    #structure   
    assert_equal("BCe", ca.sort.map {|o| o.title}.join, 'Containers content is not in proper order')
    assert_equal("Dabc", cb.sort.map {|o| o.title}.join, 'Containers content is not in proper order')
    assert_equal("d", cc.sort.map {|o| o.title}.join, 'Containers content is not in proper order')

    #search by title
    assert_equal(ca, container.find {|o| o.title =~ /A/}, "Container 'A' must be in the root container")
    assert_equal(id, cc.find {|o| o.title =~ /d/}, "Item 'd' must be in container 'C'")
    
    #childCount
    assert_equal(3, ca.childCount, "'A' has 3 children")
    assert_equal(4, cb.childCount, "'B' has 4 children")
    assert_equal(0, cd.childCount, "'D' has  no children")
    
    #indexes
    assert_equal(container[0].id, 0, 'Root container id must be 0')
    assert_raise(IndexError) {UPnP::Service::Item.new('a').id}
    assert_equal(container[6].id, 6, 'Object ID must be equal to its index')
  end
end